package com.yuelao.yuelao_backend.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.alibaba.fastjson2.annotation.JSONField;
import com.alibaba.fastjson2.JSON;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 订单实体类 - 交易整合表
 * 合并：shop_orders + coin_transactions
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("orders")
public class Order {
    
    @TableId(type = IdType.ASSIGN_ID)
    private String id;
    
    private Long userId;
    private String orderType;
    
    /** 商品信息 */
    private Long itemId;
    private String itemName;
    private String itemType;
    private Integer quantity;
    
    /** 价格信息 */
    private BigDecimal price;
    private BigDecimal totalAmount;
    private BigDecimal discount;
    private BigDecimal finalAmount;
    
    /** 支付信息 */
    private String paymentMethod;
    private Integer paymentStatus;
    private LocalDateTime paymentTime;
    private String transactionId;
    
    /** 订单状态 */
    private Integer orderStatus;
    private String statusDesc;
    
    /** 交易详情 (JSON存储) */
    @JSONField(format = "json")
    private String transactionJson;
    
    /** 时间信息 */
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;
    
    @Version
    private Integer version;
    
    /** JSON字段的Java对象映射方法 */
    public TransactionDetail getTransactionDetail() {
        return JSON.parseObject(transactionJson, TransactionDetail.class);
    }
    
    public void setTransactionDetail(TransactionDetail detail) {
        this.transactionJson = JSON.toJSONString(detail);
    }
    
    /** 内部JSON对象类 */
    @Data
    public static class TransactionDetail {
        private String coinType;
        private BigDecimal coinAmount;
        private BigDecimal balanceBefore;
        private BigDecimal balanceAfter;
        private String transactionType;
        private String description;
        private String source;
        private String target;
    }
}